/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gcalscraper.wapo;

import java.io.InputStream;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import nu.xom.Builder;
import nu.xom.Document;
import nu.xom.Nodes;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.impl.client.DefaultHttpClient;
import org.xml.sax.XMLReader;

/**
 *
 * @author dave
 */
public class DetailWorker extends Thread {
  private List<Event> detailWorkQueue;
  
  private boolean done;
  
  private Logger logger;
  
  private DefaultHttpClient httpClient;
  private Builder builder;
  
  private DetailWorkerListener ell;

  public DetailWorker(List detailWorkQueue, DetailWorkerListener ell) {
    this.detailWorkQueue = detailWorkQueue;
    this.ell = ell;
    
    logger = Logger.getAnonymousLogger();
    
    httpClient = new DefaultHttpClient();
    httpClient.getParams().setParameter(
            ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
    
    XMLReader tagsoup = new org.ccil.cowan.tagsoup.Parser(); 
    try {
      tagsoup.setFeature(
          "http://xml.org/sax/features/namespaces", false);
      tagsoup.setFeature(
          "http://xml.org/sax/features/namespace-prefixes", false);
    } catch (Exception ex) {
      logger.log(Level.SEVERE, "Trouble with tagsoup namespace settings", ex);
    }
    builder = new Builder(tagsoup);
    
  }

  @Override
  public void run() {
    while (!done) {
      Event toDo = null;
      synchronized (detailWorkQueue) {
        if (detailWorkQueue.size() > 0) {
          toDo = detailWorkQueue.remove(0);
        } else {
          try {
            detailWorkQueue.wait();
          } catch (Exception ex) {
            logger.log(Level.WARNING, "Page worker interrupted!", ex);
          }
        }        
      }
      if (toDo != null) 
        workOnEvent(toDo);
    }
  }  
  
  private void workOnEvent(Event e) {
    System.out.print(".");
    ell.startDetail(e);
    try {
      String url = "http://www.washingtonpost.com" + 
          urlEncode(e.getDetailLink());
    
      //logger.info("Getting " + url);
      HttpGet httpget = new HttpGet(url);

      HttpResponse response = httpClient.execute(httpget);
      HttpEntity entity = response.getEntity();
      InputStream is = entity.getContent();
      
      //logger.info("Got page, building DOM..");
      
      Document doc = builder.build(is);
      
      Nodes nodes = doc.query("//div[@class='eventprofiledates']");
      if (nodes.size() > 0)
        e.setDetailedDateTime(nodes.get(0));
      
      //System.out.println(e.getName() + " occurrs at " + e.getDetailedDateTime());
      
    } catch (Exception ex) {
      logger.log(Level.SEVERE, "Error getting page " + e.getDetailLink(), ex);
    }
    ell.stopDetail(e);
  }
  
  public String urlEncode(String source) {
    return source.replaceAll(" ", "%20");
  }

  public void setDone(boolean done) {
    this.done = done;
  }
  
  
}
